//
//  UIViewController+public extension.swift
//  AJiTai
//
//  Created by BO ZHOU on 2020/5/18.
//  Copyright © 2020 BO ZHOU. All rights reserved.
//

import UIKit

private var loadingView_key = "loadingView_key"
private var networkErrorView_key = "networkErrorView_key"
private var noDataView_key = "noDataView_key"


public extension UIView {
    
    
    /** 网络loadingview */
    var netLoadingView: NetWorkLoadingView? {
        get {
            if let a = objc_getAssociatedObject(self, &loadingView_key) as? NetWorkLoadingView {
                return a
            }
            return nil
        }
        set {
            
            // zwarning: OBJC_ASSOCIATION_ASSIGN 使用这个坑逼, 不能正确存储
            objc_setAssociatedObject(self, &loadingView_key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    
    
    /** 网络错误view */
    var networkErrorView: NetWorkErrorView? {
        get {
            if let a = objc_getAssociatedObject(self, &networkErrorView_key) as? NetWorkErrorView {
                return a
            }
            return nil
        }
        set {
            
            // zwarning: OBJC_ASSOCIATION_ASSIGN 使用这个坑逼, 不能正确存储
            objc_setAssociatedObject(self, &networkErrorView_key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    
    /** 无数据view */
    var noDataView: NodataView? {
        get {
            if let a = objc_getAssociatedObject(self, &noDataView_key) as? NodataView {
                return a
            }
            return nil
        }
        set {
            
            // zwarning: OBJC_ASSOCIATION_ASSIGN 使用这个坑逼, 不能正确存储
            objc_setAssociatedObject(self, &noDataView_key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    
    /** loadingview */
    func showNetWorkLoding() -> NetWorkLoadingView {
        
        if let _ = self.netLoadingView {
            
        } else {
            netLoadingView = NetworkStatusManager.getLoadingViewInstance()
            netLoadingView?.cornerRadius = self.cornerRadius
            addSubview(netLoadingView!)
        }
        
        netLoadingView!.showSelf()
        
        return netLoadingView!
    }
    
    
    func hideNetWorkLoding() {
        
        netLoadingView?.hideSelf(actualHideBlock: { [weak self] in
            guard let `self` = self else {return}
            if let _ = self.netLoadingView {
                NetworkStatusManager.cacheNetView(view: self.netLoadingView!)
                self.netLoadingView?.removeFromSuperview()
                self.netLoadingView = nil
            }

        })

    }
    
    
    func showNetWorkError(clickBlock: (()->Void)?) {
        
        if let _ = self.networkErrorView {
            
        } else {
            networkErrorView = NetworkStatusManager.getNetErrorView(buttonClickBlock: clickBlock)
            addSubview(networkErrorView!)
        }
        
        networkErrorView!.showSelf(clickBlock: clickBlock)

    }
    
    func hideNetWorkError() {
        
        networkErrorView?.hideSelf()
    }
    
    
    /** 无数据view */
    func showNodataView(_ contentString: String? = nil) {
        
        if let _ = self.noDataView {
            
        } else {
            noDataView = NetworkStatusManager.getNodataView()
            
            if contentString != nil {
                noDataView?.nodataLabel.text = contentString
            }
            addSubview(noDataView!)
        }
        
        noDataView!.showSelf()
    }
    
    /** 无数据view */
    func showNodataView(_ contentString: String? = nil, offsety: CGFloat = 0) {
        
        if let _ = self.noDataView {
            
        } else {
            noDataView = NetworkStatusManager.getNodataView()
            
            if contentString != nil {
                noDataView?.nodataLabel.text = contentString
            }
            
            noDataView?.y_offset_value = offsety
            addSubview(noDataView!)
        }
        
        noDataView!.showSelf()
    }
    
    
    func hideNodataView() {
        
        if let _ = self.noDataView {
            noDataView?.hideSelf()
            NetworkStatusManager.cacheNetView(view: self.noDataView!)
            self.noDataView?.removeFromSuperview()
            self.noDataView = nil
        }
    }
}



open class HintView: UIView {
    
    private lazy var hintLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.systemFont(ofSize: 15)
        label.textAlignment = .center
        return label
    }()
    
    
    static private(set) var shared: HintView = {
       
        let view = HintView()
        view.backgroundColor = UIColor.orange
        
        view.addSubview(view.hintLabel)
        
        view.cornerRadius = 5
        
        return view
    }()
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.hintLabel.frame = self.bounds
    }
    
    /** hintView高度 */
    let selfHeight: CGFloat = 44
    
    /** 在保证label宽度之外, 左右扩充的宽度 */
    let labelMargin: CGFloat = 20
    
    /** 显示之后多少秒消失 */
    let showTime: Double = 2
    
    /** 显示在屏幕上的起始高度比例 */
    let heightRadio: CGFloat = 0.75
    
    static func show(_ text: String!) {
        
        if text == nil {return}
        NSObject.cancelPreviousPerformRequests(withTarget: shared, selector: #selector(dismiss), object: nil)
        
        shared.alpha = 0
        shared.hintLabel.text = text
        
        if shared.superview == nil {
            KEY_WINDOW?.addSubview(shared)
        }
        
        let width = min(getLabelWidth(font: shared.hintLabel.font, height: shared.selfHeight, string: text).width + shared.labelMargin * 2, SCREEN_WIDTH - 20)
        
    
        
        shared.frame = CGRect(x: SCREEN_WIDTH/2 - width/2, y: SCREEN_HEIGHT*shared.heightRadio, width: width, height: shared.selfHeight)
        
        animation {
            shared.alpha = 1
            
            shared.perform(#selector(dismiss), with: nil, afterDelay: shared.showTime)
        }
    }
    
    @objc private func dismiss() {
        UIView.animate(withDuration: 0.25) {
            self.alpha = 0
        } completion: { (issuccess) in
            self.removeFromSuperview()
        }
    }
    
    
}

public extension UIViewController {
    
    func showHint(_ text: String?) {
        HintView.show(text)
    }
    
    func showNetWorkLoding() -> NetWorkLoadingView {
        
        return self.view.showNetWorkLoding()
    }
    
    func hideNetWorkLoding() {
        self.view.hideNetWorkLoding()
    }
    
    
    func showNetWorkError(clickBlock: (()->Void)?) {
        
        self.view.showNetWorkError(clickBlock: clickBlock)
    }
    
    func hideNetWorkError() {
        self.view.hideNetWorkError()
    }
}
